package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.IPositionBizService;
import com.kk.xhr.admin.controller.vo.PositionVO;
import com.kk.xhr.common.pojo.BinaryEnum;
import com.kk.xhr.web.vo.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 职位管理控制器
 *
 * @author kkmystery
 * @version 1.0 2021/3/5
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "职位管理", tags = "职位管理")
@RequestMapping("/position")
public class PositionController {
    private final Executor adminControllerBizExecutor;
    private final IPositionBizService positionBizService;

    @ApiOperation(value = "查询职位")
    @GetMapping
    public CompletionStage<ApiResult<List<PositionVO>>> query() {
        return CompletableFuture.supplyAsync(() -> ApiResult.success(positionBizService.query()), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加职位")
    @RequiresRoles(value = {"admin"})
    @PostMapping
    public CompletionStage<ApiResult<Void>> add(@NotBlank String name) {
        return CompletableFuture.supplyAsync(() -> {
            positionBizService.add(name);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改职位")
    @RequiresRoles(value = {"admin"})
    @PutMapping("/{id:.+}")
    public CompletionStage<ApiResult<Void>> modify(@PathVariable("id") Long id, String name) {
        return CompletableFuture.supplyAsync(() -> {
            positionBizService.modify(id, name);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改职位状态")
    @RequiresRoles(value = {"admin"})
    @PutMapping("/status/{id:.+}")
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id, BinaryEnum status) {
        return CompletableFuture.supplyAsync(() -> {
            positionBizService.modifyStatus(id, status);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}
